package com.tools.service.user.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tools.mapper.user.RoleMenuMapper;
import com.tools.pojo.user.bo.RoleMenuBO;
import com.tools.pojo.user.po.RoleMenu;
import com.tools.service.user.RoleMenuService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class RoleMenuServiceImpl extends ServiceImpl<RoleMenuMapper, RoleMenu> implements RoleMenuService {

    @Override
    @Transactional
    public Boolean addRoleMenu(RoleMenuBO roleMenuBO) {
        Long roleId = roleMenuBO.getRoleId();
        // 先删除角色的所有菜单
        lambdaUpdate().eq(RoleMenu::getRoleId, roleId).remove();


        List<RoleMenu> roleMenuList = roleMenuBO.getMenuIds().stream().map(menuId -> {
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setRoleId(roleMenuBO.getRoleId());
            roleMenu.setMenuId(menuId);
            return roleMenu;
        }).collect(Collectors.toList());



        return saveBatch(roleMenuList);
    }
}
